#include "settingformcrack.h"
#include "ui_settingformcrack.h"
#include "mainwindowcrack.h"
#include <QSettings>

SettingFormCrack::SettingFormCrack(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SettingFormCrack)
{
    ui->setupUi(this);
    this->reloadSettings();
}

SettingFormCrack::~SettingFormCrack()
{
    delete ui;
}

void SettingFormCrack::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void SettingFormCrack::fileSelected(const QString &file)
{    
    this->ui->txtPathWordFile->setText(file);
}

void SettingFormCrack::reloadSettings()
{
    QSettings   setting(kOrganization, qApp->applicationName(), this);
    bool        ret;

    setting.sync();
    this->ui->txtPrefixWord->setText(setting.value(kKeyPrefixChars, "").toString());
    this->ui->txtPostfixWord->setText(setting.value(kKeyPostfixChars, "").toString());
    this->ui->txtPathWordFile->setText(setting.value(kKeyWordFileName, "").toString());
    this->ui->spinParallelReading->setValue(setting.value(kKeyParallelThreadRead, 1).toInt(&ret));
    this->ui->spinPostfixStart->setValue(setting.value(kKeyPostfixRangeStart, 0).toInt(&ret));
    this->ui->spinPostfixEnd->setValue(setting.value(kKeyPostfixRangeEnd, 0).toInt(&ret));
    this->ui->spinPrefixStart->setValue(setting.value(kKeyPrefixRangeStart, 0).toInt(&ret));
    this->ui->spinPrefixEnd->setValue(setting.value(kKeyPrefixRangeEnd, 0).toInt(&ret));
}

void SettingFormCrack::on_cmdOpenFileWord_clicked()
{
    QFileDialog *selectFileWin = new QFileDialog(this);

    connect(selectFileWin, SIGNAL(fileSelected(QString)), this, SLOT(fileSelected(QString)));
    selectFileWin->setFilter(QDir::Files);
    selectFileWin->setNameFilter("*.txt");
    selectFileWin->show();
}

void SettingFormCrack::on_buttonBox_clicked(QAbstractButton* button)
{
    if (button->text().compare("Ok", Qt::CaseInsensitive) == 0) {
        QSettings   setting(kOrganization, qApp->applicationName(), this);
        //
        // effettua salvataggio nelle setting:
        setting.setValue(kKeyParallelThreadRead, this->ui->spinParallelReading->value());
        setting.setValue(kKeyPostfixChars, this->ui->txtPostfixWord->text().trimmed());
        setting.setValue(kKeyPostfixRangeEnd, this->ui->spinPostfixEnd->value());
        setting.setValue(kKeyPostfixRangeStart, this->ui->spinPostfixStart->value());
        setting.setValue(kKeyPrefixChars, this->ui->txtPrefixWord->text().trimmed());
        setting.setValue(kKeyPrefixRangeEnd, this->ui->spinPrefixEnd->value());
        setting.setValue(kKeyPrefixRangeStart, this->ui->spinPrefixStart->value());
        setting.setValue(kKeyWordFileName, this->ui->txtPathWordFile->text());
        setting.sync();
    }
}
